package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.CourseTypeDao;
import com.example.domain.CourseType;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.CourseTypeService;
import com.example.tool.DateTool;
import com.example.treeUtils.CourseTypeTreeUtil;
import com.example.viewInfo.CourseTypeTreeView;

@Service
public class CourseTypeServiceImpl implements CourseTypeService {

    @Autowired
    CourseTypeDao courseTypeDao;

    /**
     * @describe 添加班级类型信息
     */
    @Override
    public Result addCourseType(CourseType courseType) {

        if (courseType != null) {
            // 获取系统当前时间作为记录创建时间
            courseType.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        CourseType courseType1 = courseTypeDao.save(courseType);

        if (courseType1 != null) {
            return ResultFactory.buildSuccessResult(courseType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新课程类型信息
     */
    @Override
    public Result updateCourseType(CourseType courseType) {

        if (courseType != null) {
            // 获取系统当前时间作为记录更新时间
            courseType.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        CourseType courseType1 = courseTypeDao.saveAndFlush(courseType);

        if (courseType1 != null) {
            return ResultFactory.buildSuccessResult(courseType1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除课程类型信息
     */
    @Override
    public Result deleteCourseTypeById(String courseTypeId) {

        int row = courseTypeDao.deleteByCourseTypeId(Integer.parseInt(courseTypeId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有课程类型信息
     */
    @Override
    public Result queryAllCourseType() {

        List<CourseType> list = courseTypeDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据父ID查询课程类型名称信息
     */
    @Override
    public Result queryCourseTypeByPid(String pid) {

        List<CourseType> list = courseTypeDao.findByPid(Integer.parseInt(pid));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询课程类型信息
     */
    @Override
    public Result queryCourseTypeById(String courseTypeId) {

        List<CourseType> list = courseTypeDao.findByCourseTypeId(Integer.parseInt(courseTypeId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加课程类型信息
     */
    @Override
    public Result addCourseTypeByExcel(List<CourseType> list) {

        List<CourseType> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = courseTypeDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据课程类型编号批量删除课程类型信息
     */
    @Override
    public Result deleteCourseTypeByIds(Map<String, String> ids) {

        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = courseTypeDao.deleteByCourseTypeIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 课程类型信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        List<CourseType> list = null;

        if (info[0].equals("typeName")) {

            list = courseTypeDao.findByTypeNameLike(info[1]);

        }
        System.out.println(info.toString() + "like==========" + list);

        if (list != null) {


            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 课程类型树形结构信息查询
     */
    @Override
    public Result queryAllOfTreeForm() {
        List<CourseType> list = courseTypeDao.findAll();
        List<CourseTypeTreeView> list2 = new ArrayList<CourseTypeTreeView>();

        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                CourseTypeTreeView tv = new CourseTypeTreeView();
                tv.setCourseTypeId(list.get(i).getCourseTypeId());
                tv.setPid(list.get(i).getPid());
                tv.setTypeName(list.get(i).getTypeName());
                tv.setComment(list.get(i).getComment());
                tv.setRecordTime(list.get(i).getRecordTime());
                tv.setUpdateTime(list.get(i).getUpdateTime());

                list2.add(tv);
            }
            System.out.println(list.size() + "===============" + list2.toString());
            return ResultFactory.buildSuccessResult(CourseTypeTreeUtil.buildByRecursive(list2));
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
